#!/usr/bin/env python
# coding=utf-8
"""
@file: num_util.py
@contact: bianzhiwei@iyoujia.com
@time   : 2019/4/1 16:25 
@Desc   :
"""
import logging
import numpy as np


def percentage(row, dividend, divisor, is_int=True):
    """
    pandas de row 数据
    :param row:
    :param dividend: 被除数
    :param divisor:  除数
    :param is_int:  结果是否要整型
    :return:
    """
    try:
        d1 = row[dividend] if str(row[dividend]) != 'nan' else 0
        d2 = row[divisor] if str(row[divisor]) != 'nan' else 0
        if d2 == 0:
            return 0
        rate = round(d1 / d2 * 100) if is_int else round(d1 * 100 / d2, 2)
        if rate > 100:
            rate = '100'
    except Exception as e:
        logging.exception(e)
        rate = '0'
    return str(rate) + "%"


def difference(row, dividend, divisor, is_int=False):
    """
    差值 带百分号
    :param row:
    :param dividend:
    :param divisor:
    :param is_int:
    :return:
    """
    try:
        d1 = to_float(str(row[dividend]).replace("%", "")) if str(row[dividend]) != 'nan' else 0
        d2 = to_float(str(row[divisor]).replace("%", "")) if str(row[divisor]) != 'nan' else 0
        if d2 == 0:
            return 0
        rate = round(d1 - d2) if is_int else round(d1 - d2, 2)
    except Exception as e:
        logging.exception(e)
        rate = '0'
    return str(rate) + "%"


def contrast(row, dividend, divisor, is_int=False):
    """
    同期对比 带百分号的
     （在线房源入住率-去年同期在线房源入住率）/去年同期在线房源入住率
    :param row  DataFrame 中 的row
    :param dividend  d1 在线房源入住率
    :param divisor   d2 去年同期在线房源入住率
    :param is_int   百分比是否去整数
    :return:
    """
    try:
        d1 = to_float(str(row[dividend]).replace("%", "")) if str(row[dividend]) != 'nan' else 0
        d2 = to_float(str(row[divisor]).replace("%", "")) if str(row[divisor]) != 'nan' else 0
        if d2 == 0:
            return np.nan
        rate = round((d1 - d2) / d2 * 100) if is_int else round((d1 - d2) * 100 / d2, 2)
        if rate > 100:
            rate = '100'
    except Exception as e:
        logging.exception(e)
        rate = '0'
    return str(rate) + "%"


def divi(row, dividend, divisor, is_int=True):
    """
    两个数相除
    :param row:
    :param dividend:
    :param divisor:
    :param is_int: 结果是否要整型
    :return:
    """

    d1 = row[dividend] if str(row[dividend]) != 'nan' else 0
    d2 = row[divisor] if str(row[divisor]) != 'nan' else 0
    if d2 == 0:
        return 0
    result = round(d1 / d2) if is_int else round(d1 / d2, 2)
    return result


def float_to_int(float_num):
    if isinstance(float_num, float):
        return int(float_num)
    return float_num


def to_float(arg):
    try:
        return float(arg)
    except:
        return 0


def is_num(variable):
    try:
        float(variable)
        return True
    except Exception:
        print(type(variable), variable)
        return False


def format_num(variable, nds=2):
    if is_num(variable):
        return str(round(float(variable), nds))
    return variable


def iter_list(some_list, func):
    if not isinstance(some_list, list):
        return some_list
    return [func(value) for value in some_list]


def avg_list(num_list):
    """
    #计算平均数
    :param num_list:
    :return:
    """
    try:
        num_sum = 0
        for num in num_list:
            num_sum += num
        return round(num_sum / len(num_list), 2)
    except Exception as e:
        logging.exception(num_list)
        raise ValueError(e)


def median_list(num_list):
    """
    # 计算中位数
    :param num_list:
    :return:
    """
    num_list.sort()
    len_list = len(num_list)
    if len_list % 2 == 1:
        i = int((len_list + 1) / 2) - 1
        return round(num_list[i], 2)
    else:
        i = int(len_list / 2) - 1
        return round((num_list[i] + num_list[i + 1]) / 2, 2)


if __name__ == '__main__':
    a = ['123.324', '0', str(325.1231231), 'qwe', 1223.324, 2.12314, 12314124125131241241]
    x = iter_list(a, format_num)
    print(x)
    print(float('123.123'))
